#pragma once
#include"RBTree.h"

namespace bit
{
	template<class K, class T>
	class map
	{
		struct KeyOfT
		{
			const K& operator()(const pair<K, T>& p)
			{
				return p.first;
			}
		};

		typedef RBTree<K, pair<K, T>, KeyOfT> Tree;
	public:
		typedef typename RBTree<K, pair<K, T>, KeyOfT>::iterator iterator;
		typedef typename RBTree<K, pair<const K, T>, KeyOfT>::iterator const_iterator;
		iterator begin()
		{
			return _root.Begin();
		}

		iterator end()
		{
			return _root.End();
		}

		const_iterator cbegin()
		{
			return _root.cBegin();
		}

		const_iterator cend()
		{
			return _root.cEnd();
		}

		pair<iterator, bool> insert(const pair<K, T>& v)
		{
			return _root.Insert(v);
		}

		bool find(const K& key)
		{
			return _root.Find(key);
		}

		bool IsTrue()
		{
			return _root.IsBalance();
		}

		T& operator[](const K& key)
		{
			pair<iterator, bool> ret = _root.Insert(make_pair(key, T()));
			return ret.first->second;
		}

		/*T& operator[](const K& key)
		{
			pair<iterator, bool> ret = _root.Insert(make_pair(key, T()));
			return ret.first->second;
		}*/


	private:
		Tree _root;
	};


	void test_map1()
	{
		map<string, int> p;
		p.insert({ "1", 1 });
		p.insert({ "2", 1 });
		p.insert({ "3", 1 });
		p.insert({ "4", 1 });
		p.insert({ "5", 1 });

		cout << p.find("5") << endl;

		map<string, int>::iterator it = p.begin();
		while (it != p.end())
		{
			cout << it->first << ":" << it->second << endl;
			++it;
		}

		cout << endl;
		map<string, int> q = p;
		for (auto e : q)
		{
			cout << e.first << ":" << e.second << endl;
		}

	}

	void test_map2()
	{
		int a[] = { 4, 2, 6, 1, 3, 5, 15, 7, 16, 14,8, 3, 1, 10, 6, 4, 7, 14, 13 };
		map<int, int> mp;
		for (auto& e : a)
		{
			mp[e] = e;
		}

		for (auto& e : mp)
		{
			cout << e.first << ":" << e.second << endl;
		}

	}
}